
import {
	Utils
} from '../utils/utils';
import {
	Platform
} from '../platform/platform.js';
import {
	ColorUtils
} from '../utils/color.js';



const DEFAULT_THEME = {
	// primary: '#014C69',
	primary: '#2A87FF',
	secondary: '#2A87FF',
	danger: '#f56c6c',
	success: '#67c23a',
};

// const themeChangecallbacks = [];

const themeChangeCallbacksMap = new Map();


export class Theme {
	/** 主题 */
	static theme = Utils.clone(DEFAULT_THEME);
	/** 主题改变回调 */
	static themeChange = (callbackId, callback) => {
		if(Utils.isString(callbackId) && Utils.isFunction(callback)){
			themeChangeCallbacksMap.set(callbackId, callback);
		}
	};
	/** 删除回调 */
	static delThemeChange = (callbackId) => {
		const has = themeChangeCallbacksMap.has(callbackId);
		if(has){
			themeChangeCallbacksMap.delete(callbackId);
		}
	};
	/** 设置主题 */
	static setTheme = (theme) => {
		if (theme.primary) {
			Theme.theme.primary = theme.primary;
		}
		if (theme.secondary) {
			Theme.theme.secondary = theme.secondary;
		}
		if (theme.danger) {
			Theme.theme.danger = theme.danger;
		}
		if (theme.success) {
			Theme.theme.success = theme.success;
		}
		if (Platform.isH5) {
			document.documentElement.style.setProperty('--primary', this.theme.primary);
			document.documentElement.style.setProperty('--secondary', this.theme.secondary);
			document.documentElement.style.setProperty('--danger', this.theme.danger);
			document.documentElement.style.setProperty('--success', this.theme.success);
		}
		for (let i = 1; i < 10; i++) {
			const serial = `${10-i}`;
			const primaryA = ColorUtils.hexColorToAlpha(this.theme.primary, 0.1 * i);
			const primaryColor = ColorUtils.transparentToNonTransparent(primaryA, '#ffffff');
			Theme.theme[`primary${serial}`] = primaryColor;
			
			const secondaryA = ColorUtils.hexColorToAlpha(this.theme.secondary, 0.1 * i);
			const secondaryColor = ColorUtils.transparentToNonTransparent(secondaryA, '#ffffff');
			Theme.theme[`secondary{serial}`] = secondaryColor;

			const dangerA = ColorUtils.hexColorToAlpha(this.theme.danger, 0.1 * i);
			const dangerColor = ColorUtils.transparentToNonTransparent(dangerA, '#ffffff');
			Theme.theme[`danger${serial}`] = dangerColor;

			const successA = ColorUtils.hexColorToAlpha(this.theme.success, 0.1 * i);
			const successColor = ColorUtils.transparentToNonTransparent(successA, '#ffffff');
			Theme.theme[`success${serial}`] = successColor;

			if (Platform.isH5) {
				document.documentElement.style.setProperty(`--primary${serial}`, primaryColor);
				document.documentElement.style.setProperty(`--secondary${serial}`, secondaryColor);
				document.documentElement.style.setProperty(`--danger${serial}`, dangerColor);
				document.documentElement.style.setProperty(`--success${serial}`, successColor);
			}
		}
		/** 运行改变回调 */
		for (const callback of themeChangeCallbacksMap.values()) {
		  callback(Theme.theme);
		}
	};

}
Theme.setTheme(Utils.clone(DEFAULT_THEME))